package it.unipd.fbaron.mind_reader_squared.model;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

	@SuppressWarnings("unused")
	private final Context context;

	// Singleton
	private static DataBaseHelper istance;
	
	public static final int TRUE = 1;
	public static final int FALSE = 0;
	public static final int M1 = 0;
	public static final int M2 = 1;
	public static final int M3 = 2;
	
	public static final int INPUT_MOUSE_CLICK = 0;
	public static final int INPUT_MOUSE_LONG_CLICK = 1;
	public static final int INPUT_KEY_CLICK = 2;
	public static final int INPUT_KEY_LONG_CLICK = 3;
	public static final int INPUT_KEY_LOCATION_LEFT_CLICK = 4;
	public static final int INPUT_KEY_LOCATION_RIGHT_CLICK = 5;
	public static final int INPUT_KEY_SPACE_CLICK = 6;
	public static final int INPUT_KEY_LEFT_CLICK = 7;
	public static final int INPUT_KEY_RIGHT_CLICK = 8;
	public static final int INPUT_KEY_ENTER_CLICK = 9;
	public static final int INPUT_KEY_UP_CLICK = 10;
	public static final int INPUT_KEY_DOWN_CLICK = 11;
	public static final int INPUT_EPOC_RIGHT = 12;
	public static final int INPUT_EPOC_LEFT = 13;
	public static final int INPUT_EPOC_DOWN = 14;
	public static final int INPUT_EPOC_UP = 15;

	private static String DB_NAME = "mindReaderDatabase.db";

	public static final String PROFILE_TABLE_NAME = "profiles";
	private static final String PROFILE_TABLE_CREATE = "CREATE TABLE "
			+ PROFILE_TABLE_NAME + " ("
			+ "ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
			+ "password TEXT NOT NULL, "
			+ "first_name TEXT NOT NULL, " 
			+ "last_name TEXT NOT NULL, "
			+ "birth_date_y INTEGER NOT NULL, "
			+ "birth_date_m INTEGER NOT NULL, "
			+ "birth_date_d INTEGER NOT NULL, " 
			+ "language TEXT NOT NULL, "
			+ "preferred_mod INTEGER, " 
			+ "registration_date_y INTEGER NOT NULL, "
			+ "registration_date_m INTEGER NOT NULL, " 
			+ "registration_date_d INTEGER NOT NULL, "
			+ "preferred_language TEXT, " 
			+ "preferred_tts TEXT, "
			+ "rating INTEGER, "
			+ "open_m2 INTEGER, "
			+ "open_m3 INTEGER, "
			+ "read_all INTEGER, "
			+ "virtual_dpad INTEGER, "
			+ "input_left INTEGER, "
			+ "input_right INTEGER, "
			+ "input_select INTEGER "
			+ ");";
	public static final String SYMBOL_TABLE_NAME = "symbols";
	private static final String SYMBOL_TABLE_CREATE = "CREATE TABLE "
			+ SYMBOL_TABLE_NAME + " ("
			+ "ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
			+ "name TEXT NOT NULL, "
			+ "image_resource INTEGER NOT NULL, " 
			+ "text TEXT NOT NULL, " 
			+ "image_path TEXT NOT NULL, "
			+ "parent INTEGER, " 
			+ "m1 NUMERIC NOT NULL, " 
			+ "m1_pos INTEGER NOT NULL, "
			+ "m2 NUMERIC NOT NULL, " 
			+ "m2_pos INTEGER NOT NULL, "
			+ "sound_path TEXT NOT NULL, "
			+ "profile INTEGER NOT NULL, "
			+ "clicks_m1 INTEGER, "
			+ "sound_choise NUMERIC NOT NULL, " //0->text to speech   1->recorded sound
			+ "clicks_m2 INTEGER, "
			+ "FOREIGN KEY (profile) REFERENCES " + PROFILE_TABLE_NAME
			+ "(ID) ON DELETE CASCADE ON UPDATE CASCADE, "
			+ "FOREIGN KEY (parent) REFERENCES "
			+ SYMBOL_TABLE_NAME + "(ID) ON DELETE CASCADE ON UPDATE CASCADE"
			+ ");";

	/**
	 * Constructor Takes and keeps a reference of the passed context in order to
	 * access to the application assets and resources.
	 * 
	 * @param context
	 */
	private DataBaseHelper(Context context) {
		super(context, DB_NAME, null, 1);
		this.context = context;
	}

	public static DataBaseHelper getInstance(Context context) {
		if (istance == null) {
			istance = new DataBaseHelper(context);
		}
		return istance;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(PROFILE_TABLE_CREATE);
		db.execSQL(SYMBOL_TABLE_CREATE);
	}
	
	@Override
	public void onOpen(SQLiteDatabase db) {
		/*db.execSQL("DROP TABLE symbols");
		db.execSQL("DROP TABLE profiles");
		db.execSQL(PROFILE_TABLE_CREATE);
		db.execSQL(SYMBOL_TABLE_CREATE);*/
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	
	
	/************************************
	 ************** UTILS ***************
	 ***********************************/
	
	public static Date intToDate(int y, int m, int d) {
		return new Date(y, m, d);
	}
	
	
	public static String cleanStringForSave(String textToClean) {
		return textToClean.replace("'", "''");
	}
	
	public static String cleanStringForLoad(String textToClean) {
		return textToClean.replace("''", "'");
	}
	
	public static String md5(String s) {
	    try {
	        // Create MD5 Hash
	        MessageDigest digest = java.security.MessageDigest.getInstance("MD5");
	        digest.update(s.getBytes());
	        byte messageDigest[] = digest.digest();
	        
	        // Create Hex String
	        StringBuffer hexString = new StringBuffer();
	        for (int i=0; i<messageDigest.length; i++)
	            hexString.append(Integer.toHexString(0xFF & messageDigest[i]));
	        return hexString.toString();
	        
	    } catch (NoSuchAlgorithmException e) {
	        e.printStackTrace();
	    }
	    return "";
	}
}